using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace Microsoft.WPF.Samples
{
    /// <summary>
    /// This Class will convert the Gender Property of the Person Class into a 
    /// SolidColorBrush
    /// </summary>
    [ValueConversion(typeof(String), typeof(SolidColorBrush))]
    public class GenderToSolidColorBrush 
        : IValueConverter
    {
        #region Convert()
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.
        /// </param>
        /// <param name="targetType">The type of the binding target property.
        /// </param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value 
        /// is used.
        /// </returns>
        public object Convert(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            switch (value.ToString())
            {
                case "male":
                    return GetSolidColorBrushFromRGB(0, 153, 204);

                case "female":
                    return GetSolidColorBrushFromRGB(255, 102, 255);

                default:
                    return GetSolidColorBrushFromRGB(150, 150, 150);
            }
        } 
        #endregion

        #region ConvertBack()
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding
        ///  target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value 
        /// is used.
        /// </returns>
        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            return null;
        } 
        #endregion

        #region GetSolidColorBrush()
        /// <summary>
        /// RGBs the color.
        /// </summary>
        /// <param name="red">The red.</param>
        /// <param name="green">The green.</param>
        /// <param name="blue">The blue.</param>
        /// <returns>a SolidColorBrush</returns>
        private static SolidColorBrush GetSolidColorBrushFromRGB(
            byte red, 
            byte green, 
            byte blue)
        {
            var brush = new SolidColorBrush(Color.FromRgb(red, green, blue));
            return brush;
        } 
        #endregion

    }
}